package com.choudou5.spider.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * 爬虫json工具类
 *
 * @author choudou5
 * @create 2017-11-上午 11:27
 **/
public class SpiderJsonUtil {


    public static void main(String[] args) {
        String msg = "我在北京天安门，我在北京很兴奋，来北京真好";
        int index = StringUtils.ordinalIndexOf(msg, "北京", 4);
        System.out.println(index);

        String ruleStr = "我在北京|2|";
        String[] params = StringUtils.split(ruleStr, "|", 3);
        System.out.println(params.length);
    }

    private static void test(int i) {
        System.out.println(i);
    }

    /**
     * 获取 拼接字段值
     *
     * @param ruleKey eg:/category/$id$.html?title=$title$
     * @return
     */
    public static String getJoinFieldValue(JSONObject jsonObj, String ruleKey) {
        String[] fields = getJoinFields(ruleKey);
        if (ArrayUtils.isNotEmpty(fields)) {
            String value = null;
            for (String field : fields) {
                value = formatJsonVal(jsonObj, field);
                ruleKey = StringUtils.replace(ruleKey, "$" + field + "$", value);
            }
            return ruleKey;
        }
        return formatJsonVal(jsonObj, ruleKey);
    }


    /**
     * 是否为 拼接字段
     *
     * @param ruleKey eg:/category/$id$.html?title=$title$
     * @return
     */
    public static String[] getJoinFields(String ruleKey) {
        char[] chars = ruleKey.toCharArray();
        StringBuilder fields = new StringBuilder(64);
        boolean append = false;
        int tagCount = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '$') {
                append = true;
                tagCount++;
                if (tagCount > 0 && tagCount % 2 == 0) {
                    append = false;
                    fields.append(",");
                }
                continue;
            }
            if (append)
                fields.append(chars[i]);
        }
        return fields.toString().split(",");
    }

    /**
     * 是否为 拼接字段
     *
     * @param ruleKey eg:/category/$id$.html?title=$title$
     * @return
     */
    public static boolean isJoinField(String ruleKey) {
        char[] chars = ruleKey.toCharArray();
        int count = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '$')
                count++;
        }
        return count > 1;
    }

    public static String formatJsonVal(JSONObject jsonObject, String ruleKey) {
        Object obj = null;
        //是取含子 字段
        if(ruleKey.contains(".")){
            String[] arr = StringUtils.split(ruleKey, ".");
            obj = jsonObject.getJSONObject(arr[0]).get(arr[1]);
        }else{
            obj = jsonObject.get(ruleKey);
        }
        String value = null;
        if (obj != null && obj instanceof BigDecimal) {
            //检查 是否为整数
            value = checkIsIntegerValue((BigDecimal) obj);
        } else {
            value = obj.toString();
        }
        return value;
    }

    //检查 是否为整数
    private static String checkIsIntegerValue(BigDecimal obj) {
        if (obj.scale() == 1 && obj.toString().endsWith("0"))
            return obj.intValue() + "";
        if (obj.scale() == 2 && obj.toString().endsWith("00"))
            return obj.intValue() + "";
        return obj.toString();
    }

}
